<template>
  <Dialog :title="t('interview.index.selectStudent')" v-model="dialogVisible" width="1400">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item :label="t('student.fields.studentName')" prop="studentName">
          <el-input
            v-model="queryParams.studentName"
            :placeholder="t('common.inputTextPlaceholder') + t('student.fields.studentName')"
            clearable
            @keyup.enter="handleQuery"
            class="!w-160px"
            :disabled="advancedSearching"
          />
        </el-form-item>
        <el-form-item :label="t('student.fields.phone')" prop="phone">
          <el-input
            v-model="queryParams.phone"
            :placeholder="t('common.inputTextPlaceholder') + t('student.fields.phone')"
            clearable
            @keyup.enter="handleQuery"
            class="!w-160px"
            :disabled="advancedSearching"
          />
        </el-form-item>
        <el-form-item :label="t('student.fields.interviewSelected')" prop="interviewSelected">
          <el-select
            v-model="queryParams.interviewSelected"
            :placeholder="t('common.selectTextPlaceholder') + t('student.fields.interviewSelected')"
            clearable
            class="!w-160px"
            :disabled="advancedSearching"
            @change="handleQuery"
          >
            <el-option :label="t('common.yes')" :value="true" />
            <el-option :label="t('common.no')" :value="false" />
          </el-select>
        </el-form-item>
        <el-form-item :label="t('student.fields.studyMajor')" prop="studyMajor">
          <el-input
            v-model="queryParams.studyMajor"
            :placeholder="t('common.inputTextPlaceholder') + t('student.fields.studyMajor')"
            clearable
            @keyup.enter="handleQuery"
            class="!w-160px"
            :disabled="advancedSearching"
          />
        </el-form-item>
        <el-form-item :label="t('student.fields.applyMajor')" prop="applyMajor">
          <el-input
            v-model="queryParams.applyMajor"
            :placeholder="t('common.inputTextPlaceholder') + t('student.fields.applyMajor')"
            clearable
            @keyup.enter="handleQuery"
            class="!w-160px"
            :disabled="advancedSearching"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery" :disabled="advancedSearching">
            <Icon icon="ep:search" class="mr-5px" />
            {{ t('common.search') }}
          </el-button>
          <el-button @click="resetQuery" :disabled="advancedSearching">
            <Icon icon="ep:refresh" class="mr-5px" />
            {{ t('common.reset') }}
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <vxe-table
        ref="tableRef"
        v-loading="loading"
        :data="list"
        stripe
        :row-config="{ keyField: 'id' }"
        :checkbox-config="{
          reserve: true,
          checkMethod: ({ row }) => row.interviewSelected === false
        }"
        :column-config="{ resizable: true }"
        :size="vxeTableSize"
        @checkbox-all="collectSelect"
        @checkbox-change="collectSelect"
      >
        <vxe-column type="checkbox" width="50" align="center" fixed="left" />
        <vxe-column
          :title="t('student.fields.studentName')"
          field="studentName"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
          fixed="left"
        />
        <vxe-column
          :title="t('student.fields.phone')"
          field="phone"
          align="center"
          show-header-overflow
          show-overflow
          min-width="120"
        />
        <vxe-column
          :title="t('student.fields.batchId')"
          field="batchId"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        >
          <template #default="scope">
            {{ batchOptions.find((item) => item.id === scope.row.batchId)?.batchName }}
          </template>
        </vxe-column>
        <vxe-column
          :title="t('student.fields.interviewTime')"
          field="interviewTime"
          align="center"
          show-header-overflow
          show-overflow
          min-width="120"
        />
        <vxe-column
          :title="t('student.fields.interviewSelected')"
          field="interviewSelected"
          align="center"
          show-header-overflow
          show-overflow
          min-width="130"
        >
          <template #default="scope">
            {{ scope.row.interviewSelected ? t('common.yes') : t('common.no') }}
          </template>
        </vxe-column>
        <vxe-column
          :title="t('student.fields.interviewSlotTime')"
          field="interviewSlotTime"
          align="center"
          show-header-overflow
          show-overflow
          min-width="140"
        />
        <vxe-column
          :title="t('student.fields.interviewGroupId')"
          field="groupName"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.university')"
          field="university"
          align="center"
          show-header-overflow
          show-overflow
          min-width="140"
        />
        <vxe-column
          :title="t('student.fields.studyMajor')"
          field="studyMajor"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.applyMajor')"
          field="applyMajor"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
      </vxe-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>

    <template #footer>
      <el-button @click="handleNext" type="primary" :disabled="formLoading"
        >{{ t('common.nextLabel') }}
      </el-button>
      <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import {
  RecruitStudentGroupingListRespVO,
  StudentApi,
  StudentVO
} from '@/api/recruit/recruitstudent'
import type { VxeTableInstance, VxeTablePropTypes, VxeToolbarInstance } from 'vxe-table'
import { useAppStore } from '@/store/modules/app'
import { BatchApi, BatchVO } from '@/api/recruit/batch'
import { InterviewVO } from '@/api/recruit/interview'
import { vxeBooleanFormatter } from '@/utils/formatter'

/** 报名学生 列表 */
defineOptions({ name: 'RecruitStudentSelect' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const toolbarRef = ref<VxeToolbarInstance>()
const tableRef = ref<VxeTableInstance>()

const loading = ref(true) // 列表的加载中
const list = ref<RecruitStudentGroupingListRespVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const advancedSearching = ref(false) // 高级搜索
const dialogVisible = ref(false)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  advanced: undefined,
  accountId: undefined,
  studentName: undefined,
  recruitType: undefined,
  phone: undefined,
  batchId: undefined,
  recruitStatus: undefined,
  recruitTime: [],
  interviewQuality: undefined,
  interviewSelected: undefined,
  interviewDate: [],
  interviewTime: undefined,
  interviewStatus: undefined,
  interviewId: undefined,
  interviewGroupId: undefined,
  interviewSlotId: undefined,
  interviewSlotTime: undefined,
  interviewScore: undefined,
  chioceStatus: undefined,
  chioceMentorId: undefined,
  birthdate: [],
  gender: undefined,
  politicalStatus: undefined,
  ethnic: undefined,
  identityNumber: undefined,
  contactAddress: undefined,
  email: undefined,
  university: undefined,
  is985: undefined,
  is211: undefined,
  isFirstClass: undefined,
  studyMajor: undefined,
  applyMajor: undefined,
  majorRank: undefined,
  majorTotal: undefined,
  classRank: undefined,
  classTotal: undefined,
  cet4Score: undefined,
  cet6Score: undefined,
  attachment: undefined,
  remark: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const emits = defineEmits<{
  (e: 'next', value: StudentVO[]): void
}>()

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

const selectListObject = ref<StudentVO[]>([])

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await StudentApi.getStudentGroupingList(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  advancedSearching.value = false
  ;(queryParams as any).advanced = undefined
  queryParams.batchId = currentInterview.value?.batchId as any
  queryParams.interviewId = currentInterview.value?.id as any
  handleQuery()
}

const collectSelect = () => {
  selectListObject.value = [
    ...(tableRef.value?.getCheckboxRecords() ?? []),
    ...(tableRef.value?.getCheckboxReserveRecords() ?? [])
  ] as StudentVO[]
}

const formLoading = ref(false)

const batchOptions = ref<BatchVO[]>([])
const getBatchSelectOptions = async () => {
  batchOptions.value = await BatchApi.getSelectList()
}

const currentInterview = ref<InterviewVO>()

// 面试管理-设置分组 新增第一步 弹出学生列表
const open = async (interview: InterviewVO) => {
  currentInterview.value = interview
  dialogVisible.value = true
  await nextTick()
  resetQuery()
}

const handleNext = () => {
  if (selectListObject.value.length === 0) {
    message.error(t('student.groupSelect.noData'))
    return
  }
  emits('next', selectListObject.value)
  dialogVisible.value = false
  selectListObject.value = []
}

/** 初始化 **/
onMounted(async () => {
  // 查询所有计划
  await getBatchSelectOptions()
})

defineExpose({
  open
})
</script>

<style lang="scss" scoped>
:deep(.vxe-cell--checkbox.is--disabled) {
  .vxe-table-icon-checkbox-unchecked:before {
    content: '\e67d';
  }
}
</style>
